package com.demo.amusic.dao;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.demo.amusic.entity.Music;

import java.util.ArrayList;
import java.util.List;

/**
 * 音乐库DAO
 */
public class MusicDao {
    private static final String TAG = "MusicDao";
    private Context context;
    private static MusicDao instance;
    // 操作表的对象，进行增删改查
    private SQLiteDatabase writableDatabase;

    private MusicDao(Context context) {
        this.context = context;
        DBHelper dbHelper = new DBHelper(context);
        writableDatabase = dbHelper.getWritableDatabase();
    }

    public static MusicDao getInstance(Context context) {
        if (instance == null) {
            synchronized (MusicDao.class) {
                if (instance == null) {
                    instance = new MusicDao(context);
                }
            }
        }
        return instance;
    }

    /**
     * 查询全部音乐
     */
    public List<Music> list() {
        List<Music> result = new ArrayList<>();

        Cursor cursor = writableDatabase.query("music", null, null, null, null, null, null, null);

        int position = cursor.getPosition();
        Log.e(TAG, "select: 游标默认位置：" + position);

        while (cursor.moveToNext()) {
            Music music = new Music();

            @SuppressLint("Range") int id = cursor.getInt(cursor.getColumnIndex("id"));
            @SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex("name"));
            @SuppressLint("Range") String author = cursor.getString(cursor.getColumnIndex("author"));
            @SuppressLint("Range") String url = cursor.getString(cursor.getColumnIndex("url"));
            @SuppressLint("Range") String localUrl = cursor.getString(cursor.getColumnIndex("localurl"));
            @SuppressLint("Range") int type = cursor.getInt(cursor.getColumnIndex("type"));

            music.setId(id);
            music.setName(name);
            music.setAuthor(author);
            music.setUrl(url);
            music.setLocalUrl(localUrl);
            music.setType(type);
            result.add(music);

        }

        return result;
    }


    /**
     * 添加或更新音乐
     */
    public long addOrUpdata(Music music){
        ContentValues contentValues = new ContentValues();

        if (music.getId()>0){
            int id = music.getId();
            contentValues.put("id", id);
        }

        String name = music.getName();
        contentValues.put("name", name);

        String author = music.getAuthor();
        contentValues.put("author", author);

        String url = music.getUrl();
        contentValues.put("url",url);

        String localUrl = music.getLocalUrl();
        contentValues.put("localUrl",localUrl);

        int type = music.getType();
        contentValues.put("type",type);

        long result = 0;
        if (music.getId()>0){//修改
            result = writableDatabase.update("music", contentValues, "id = ?", new String[]{music.getId()+""});
        }else{
            result = writableDatabase.insert("music", null, contentValues);
        }


        return result;
    }

    /**
     * 删除音乐
     */
    public long delete(int id){
        return writableDatabase.delete("music", "id = ?", new String[]{id + ""});
    }
}

